<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/4/15
 * Time: 17:11
 */

namespace app\mobile\controller\user;

class Used extends Common
{
    private $mod;
    public function _initialize()
    {
        parent::_initialize();
       $this->mod = model('second_house');
        $this->_name = 'SecondHouse';
    }
    public function index(){
        $where['user_id'] = $this->userInfo['id'];
        $lists = $this->mod->where($where)->field('id,title,city_name,area_name,hits,status,create_time')->order('id desc')->paginate(10);
        $this->assign('lists',$lists);
        $this->assign('pages',$lists->render());
        $this->assign('title','我的二手房');
        return $this->fetch();
    }
    public function message(){
        $where['uid'] = $this->userInfo['id'];
        $where['model'] = 'second_house';
        $lists        = model('baoming')->where($where)->field('id,house_id,house_title,name,phone,status,type,model,create_time')->paginate(15);
        $this->assign('lists',$lists);
        $this->assign('pages',$lists->render());
    return $this->fetch();
    }
    public function editMsg(){
        $id = input('param.id/d',0);
        $where['id']  = $id;
        $where['uid'] = $this->userInfo['id'];
        $info = model('baoming')->where($where)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function editMsgDo(){
        $data['status'] = input('post.status/d',0);
        $data['info']   = input('post.info');
        $where['id']    = input('post.id');
        $where['uid']   = $this->userInfo['id'];
        if(model('baoming')->save($data,$where)){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }
    public function addDo(){
        \app\common\model\SecondHouse::event('before_insert',function($obj){
            $city = cache('city');
            if($obj->area){
                $area_arr  = explode(',',$obj->area);
                $obj->city = $area_arr[0];
                $obj->city_name = $city['cate'][$area_arr[0]]['name'];
                $obj->area      = isset($area_arr[1])?$area_arr[1]:0;
                $obj->area_name = isset($area_arr[1])?$city['cate'][$area_arr[1]]['name']:'';
            }
            $map       = explode(',',input('post.map'));
            $obj->lat  = isset($map[1])?$map[1]:0;
            $obj->lng  = (isset($map[0]) && !empty($map[0]))?$map[0]:0;
            $obj->house_type         = implode(',',$obj->house_type);
            $obj->peitao    = isset($obj->peitao)?implode(',',$obj->peitao):'';
            $obj->user_id   = $this->userInfo['id'];
            $obj->user_type = $this->userInfo['model'];
            $obj->info = preg_replace('/<a[^>]*>|<\/a>/isU','',$obj->info);
            if($obj->status==1){
                $obj->status = cache('user')['second'];
            }
            $images = $this->getPic();
            if($images){
                $obj->file = $images;
            }
            return true;
        });
        \app\common\model\SecondHouse::event('after_insert',function($obj){
            $obj->estate_id > 0 && model('estate')->where(['id'=>$obj->estate_id])->setInc('second_num');
            model('user')->where(['id'=>$this->userInfo['id']])->setInc('chushou');
        });
        parent::addDo();
    }
    public function editDo(){
        \app\common\model\SecondHouse::event('before_update',function($obj){
            $city = cache('city');
            if($obj->area){
                $area_arr  = explode(',',$obj->area);
                $obj->city = $area_arr[0];
                $obj->city_name = $city['cate'][$area_arr[0]]['name'];
                $obj->area      = isset($area_arr[1])?$area_arr[1]:0;
                $obj->area_name = isset($area_arr[1])?$city['cate'][$area_arr[1]]['name']:'';
            }
            $map       = explode(',',input('post.map'));
            $obj->lat  = isset($map[1])?$map[1]:0;
            $obj->lng  = (isset($map[0]) && !empty($map[0]))?$map[0]:0;
            $obj->house_type         = implode(',',$obj->house_type);
            $obj->peitao    = isset($obj->peitao)?implode(',',$obj->peitao):'';
            $obj->info = preg_replace('/<a[^>]*>|<\/a>/isU','',$obj->info);
            if(isset($obj->status) && $obj->status==1){
                $obj->status = cache('user')['second'];
            }
            $images = $this->getPic();
            if($images){
                $obj->file = $images;
            }
            return true;
        });
        parent::editDo();
    }
    public function delete(){
        //删除后置操作
        \app\common\model\SecondHouse::event('after_delete',function($obj){
            model('estate')->where(['id'=>$obj->estate_id])->setDec('second_num');
            model('user')->where(['id'=>$this->userInfo['id']])->setDec('chushou');
        });
        parent::delete();
    }
    /**
     * @param $obj
     * 添加图片
     */
    private function getPic(){
        $insert = [];
        if(isset($_POST['pic']) && !empty($_POST['pic'])) {
            $images = $_POST['pic'];
            foreach ($images['pic'] as $key => $v) {
                $insert[] = [
                    'url' => $v,
                    'title' => $images['alt'][$key],
                ];
            }
        }
        return $insert;
    }
    public function ajaxGetEstate(){
        $area_id = input('get.area_id',0);
        $arr     = explode(',',$area_id);
        $return['status']   = 0;
        $where['city'] = $arr[0];
        $where['status']    = 1;
        if(isset($arr[1])){
            $where['area'] = $arr[1];
        }
        $estate = model('estate')->where($where)->field('id,title')->select();
        if($estate){
            $return['status'] = 1;
            $return['data']   = objToArray($estate);
        }
        return json($return);
    }
    public function ajaxGetEstateInfo(){
        $id = input('get.id/d',0);
        $return['status'] = 1;
        $where['status'] = 1;
        $where['id'] = $id;
        $info = model('estate')->where($where)->field('id,title,niandai,lat,lng,address')->find();
        if($info){
            $return['status'] = 1;
            $info['map']      = $info['lng'].','.$info['lat'];
            $return['data']   = objToArray($info);
        }
        return json($return);
    }
}